<?php
/** Une classe représentant les groupes utilisateurs
 * Auteur : Sebastian Mosser
 */

class Group implements iPersistantData, iHTMLViewable, iLookable {

	private $_id;
	private $_label;
	
	public function __construct($label, $id = -1) {
		$this->_id    = $id;
		$this->_label = $label;
	}

	/** Implementation de l'interface iPersistantData **/

	public static function createTable() {
		$query  = "CREATE TABLE IF NOT EXISTS `group` (\n";
		$query .= "  `id`    INTEGER     NOT NULL PRIMARY KEY auto_increment, \n"; 
		$query .= "  `label` VARCHAR(30) NOT NULL);\n ";
		
		$myDb = Database::getSingleton();
		$myDb->execute($query);
		
	}
	
	public static function dropTable() {
		$query = "DROP TABLE IF EXISTS `group`;";
		$myDb = Database::getSingleton();
		$myDb->execute($query);
	}
	
	public  function create() {
		if ($this->_id != -1)
			throw new RuntimeException("Group/create()",
												"This object is still present in database !");
		$label = Database::escape($this->_label);
		$query  = "INSERT INTO `group` VALUES (\n";
		$query .= "  '', '$label'); ";
		
		$myDb = Database::getSingleton();
		
		/**
		$myDb->execute($query);
		($myDb->lastKeyValue() != 0 ? $this->_id = $myDb->lastKeyValue() : $this->_id = -1);
		**/
		$result = $myDb->executeInsert($query);
		if ($result[0] != 0)
			$this->_id = $result[0];
		else
			$this->id = -1;
		
	}
	
	public static function read($key) {
		$key = Database::escape($key);
		$query = "SELECT * FROM `group` WHERE `id` = $key;";
		$myDb = Database::getSingleton();
		$ans = Database::assoc($myDb->execute($query));
		if (! $ans)
			throw new RuntimeException("Group/read()","Unknown '$key'");

		return new Group($ans["label"],$ans["id"]);
	}

	public function update() {
		if ($this->_id == -1)
			throw new RuntimeException("Group/update()",
												"This object wasn't saved in database !");
		$label = Database::escape($this->_label);
		$query = "UPDATE `group` SET `label` = '$label' WHERE `id` = $this->_id";
		$myDb = Database::getSingleton();
		$myDb->execute($query);
	}
	
	// supression d'un element
	public function delete() {
		if ($this->_id == -1)
			throw new RuntimeException("Group/delete()",
												"This object wasn't saved in database !");
		$label = Database::escape($this->_label);
		$query = "DELETE FROM `group` WHERE `id` = $this->_id";
		$myDb = Database::getSingleton();
		$myDb->execute($query);
	
	}
	
	public function tag() {
	 		$tmp = array();
	 		$tmp[0] = "Group";
	 		$tmp[1] = $this->_id;
	 		return $tmp;
	 }
	public function soundsLike($o) {
	 		return ($this->_label == $o->_label);
	 	}

	/** Fin de l'implementation de l'interface iPersistantData **/

	/** Implementation de l'interface iHTMLViewable **/
	
	public function displayLong() {
		return "$this->_label [$this->_id]";
	}
	
	public function displayShort() {
		return $this->_label;
	}
	
	public function displayInline($scriptUrl = "") {
		return "<td> $this->_id </td><td> $this->_label </td>";
	}
	
	/** Fin de l'implementation de l'interface HTMLViewable **/


   /** Implémentation de l'interface de recherche **/
	public static function lookFor($filter) {
		$filter = Database::escape($filter);
		$myDb   = Database::getSingleton();
		$query  = "SELECT * FROM `group` WHERE `label` LIKE '$filter' ORDER BY `label`;";
		$ans    = $myDb->execute($query);
		return $ans;
	}
	
	public static function showResult($resultSet,$scriptUrl = ""){ 
      $result  = "<div align=\"center\">\n";
      $result .= "  <table class=\"resultset\">\n";
      $result .= "    <tr><th>Identifiant ACL</th><th>Nom du groupe</th></tr>\n";
      
	   while (($row = Database::assoc($resultSet)) != False) {
	      $result .= "    <tr>". Group::read($row["id"])->displayInline()."</tr>\n";
	   }
	   
	   $result .= "  </table>\n</div>";
	   return $result;
	}   
   /** Fin de l'implementation de l'interface de recherche **/
   

}

?>